package com.pig4cloud.pigx.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.utils.id.IdWorker;
import com.pig4cloud.pigx.system.dto.ArticleInformationDTO;
import com.pig4cloud.pigx.system.entity.Article;
import com.pig4cloud.pigx.system.entity.ArticleDetails;
import com.pig4cloud.pigx.system.entity.ArticleInformation;
import com.pig4cloud.pigx.system.mapper.ArticleManagementMapper;
import com.pig4cloud.pigx.system.mapstruct.ArticleInformationStruct;
import com.pig4cloud.pigx.system.service.ArticleDetailsService;
import com.pig4cloud.pigx.system.service.ArticleManagementService;
import com.pig4cloud.pigx.system.service.ArticleService;
import com.pig4cloud.pigx.system.vo.EmailInformationVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Description:文章管理，处理所有文章（公告、帮助、Banner、联系我们。。。）
 * @Date: 2022/8/18 16:53
 */
@Service
@RequiredArgsConstructor
public class ArticleManagementServiceImpl implements ArticleManagementService {
    /**
     * 文章列表
     */
    private final ArticleService articleService;
    /**
     * 文章详情
     */
    private final ArticleDetailsService articleDetailsService;
    /**
     * 文章管理
     */
    private final ArticleManagementMapper articleManagementMapper;
    /**
     * 雪花id
     */
    private final IdWorker idWorker;

    /**
     * 分页查询
     *
     * @param page
     * @param articleInformation
     * @param param              排序参数
     * @return
     */
    @Override
    public R page(Page page, ArticleInformation articleInformation, Map<String, Object> param) {
        //查询文章信息
        List<ArticleInformation> records = articleManagementMapper.getPage(page, articleInformation, param);
        //查询总条数
        Integer count = articleManagementMapper.getCount(articleInformation);
        //总条数
        page.setTotal(count);
        //总页数
        page.setPages((count + page.getSize() - 1) / page.getSize());
        //参数
        page.setRecords(records);
        return R.ok(page);
    }

    /**
     * 通过id查询文章列表及文章详情
     *
     * @param id id
     * @return R
     */
    @Override
    public R getArticleById(Long id) {
        ArticleInformation articleInformation = new ArticleInformation();
        articleInformation.setId(id);
        return R.ok(articleManagementMapper.getPage(null, articleInformation, null).get(0));
    }

    /**
     * 新增文章列表及文章详情
     *
     * @param articleInformation 文章列表
     * @return R
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public R saveArticleInformation(ArticleInformation articleInformation) {
        articleInformation.setId(idWorker.nextId());
        articleInformation.setCreateTime(LocalDateTime.now());
        articleService.save(new Article(articleInformation));
        articleDetailsService.save(new ArticleDetails(articleInformation));
        return R.ok(true);
    }

    /**
     * 修改文章列表及文章详情
     *
     * @param articleInformation 文章列表及文章详情
     * @return R
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public R updateById(ArticleInformation articleInformation) {
        articleInformation.setUpdateDate(LocalDateTime.now());
        articleService.updateById(new Article(articleInformation));
        articleDetailsService.update(new ArticleDetails(articleInformation), new QueryWrapper<ArticleDetails>()
                .eq("article_id", articleInformation.getId()));
        return R.ok(true);
    }

    /**
     * 通过id删除文章列表
     *
     * @param id id
     * @return R
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public R removeById(Long id) {
        articleService.removeById(id);
        articleDetailsService.remove(new QueryWrapper<ArticleDetails>()
                .eq("article_id", id));
        return null;
    }

    /**
     * 获取文章详情
     *
     * @param articleInformation 文章列表
     * @return
     */
    @Override
    public R<List<ArticleInformation>> getArticleManagement(ArticleInformation articleInformation, Map<String, Object> param) {
        // 查询文章信息
        return R.ok(articleManagementMapper.getPage(null, articleInformation, param));
    }

    /**
     * 获取邮件内容详情
     *
     * @param articleInformationDTO 文章列表
     * @return
     */
    @Override
    public R<List<EmailInformationVO>> getEmailManagement(ArticleInformationDTO articleInformationDTO, Map<String, Object> param) {
        // 查询文章信息
        List<ArticleInformation> articleInformationList = articleManagementMapper.getPage(
                null, ArticleInformationStruct.INSTANCE.dto2ArticleInformation(articleInformationDTO), param);
        // 类型转换
        List<EmailInformationVO> emailInformationVOList =
                ArticleInformationStruct.INSTANCE.articleInformationList2EmailInformationVOList(articleInformationList);
        return R.ok(emailInformationVOList);
    }
}

